<?php
//商品类别管理
namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Requests;

class GenreController extends CommonController
{
    //商品类别管理浏览
    public function index()
    {
        $db = \DB::table("m_genre");
		//判断并封装搜索条件
		$params = array();
		if(!empty($_GET['name'])){
			$db->where("name","like","%{$_GET['name']}%");
			$params['name'] = $_GET['name'];  //维持搜索条件
		}
		$data = $db->orderBy('fid', 'asc')->paginate(7);
		$data1 = \DB::table("m_forum")->get();
		return view("admin.genre.index",['list'=>$data,'list1'=>$data1,'params'=>$params]);
    }
	//商品类别添加操作界面
    public function create()
    {
       $data = \DB::table("m_forum")->get();
	   return view('admin.genre.add',['list'=>$data]);
    }
	//商品类别后台添加
    public function store(Request $request)
    {
		$data = $request->all();
		$data = $request->except('_token');
		if(empty($data['name'])){
			$info = "请输入商品类别信息！！";
		}else{
			$d = \DB::table("m_genre")->where("name",$data['name'])->first();
			if(empty($d)){
				$id = \DB::table("m_genre")->insertGetId($data);
				$info = "商品类别添加成功！";
			}else{
				$info = "该商品类别已存在！";
			}
		}
		return redirect('admin/genre')->with("err",$info);
    }
    public function show($id)
    {
        //
    }
    public function edit($id)
    {
		$data = \DB::table("m_genre")->where('id',$id)->first();
		$data1 = \DB::table("m_forum")->get();
		return view("admin.genre.edit",['list'=>$data,'list1'=>$data1]);
    }
    public function update(Request $request, $id)
    {
        $data =	$request->all();
		$data = $request->except('_token','_method');
		if(empty($data['name'])){
			$info = "修改值不能为空!!";
		}else{
			\DB::table('m_genre')->where('id',$id)->update($data);
			$info = "商品类别修改成功！";
		}
		return redirect('admin/genre')->with("err",$info);
    }
    public function destroy($id)
    {
		$data = \DB::table("m_search")->where('gid',$id)->get();
		//判断商品类别下是否没有下属商品
		if(empty($data)){
			\DB::table('m_genre')->where('id', $id)->delete();
			$info = "商品类别删除成功！";
		}else{
            $info = "存在下属商品，无法删除！";
        }
        return redirect('admin/genre')->with("err",$info);
    }
}
